import {SkuCode} from "./SkuCode";
import {CellStatus} from "../../core/enum";
import {SkuPending} from "./sku-pending";
import {Joiner} from "../../utils/Joiner";
import {Cell} from "./cell";

/**
 * 法官 审判者
 */
class Judger {

    fenceGroup
    // spu 字典
    pathDict = []
    skuPeding

    constructor(fenceGroup) {
        this.fenceGroup = fenceGroup
        this.initPathDict()
        this._initSkuPending()
    }


    _initSkuPending() {
        // 规格长度
        const specsLength = this.fenceGroup.fences.length
        this.skuPeding = new SkuPending(specsLength);
        const defaultSku = this.fenceGroup.getDefaultSku()
        if (!defaultSku) {
            return
        }
        this.skuPeding.init(defaultSku)
        // 计算默认 sku 的规格信息
        this.skuPeding.pending.forEach(cell => {
            this.fenceGroup.setCellStatusById(cell.id, CellStatus.SELECTED)
        })
        this.judge(null, null, null, true)
        console.log(this.skuPeding)
    }

    // _initSkuPending() {
    //   this.skuPeding = new SkuPending()
    // }

    isSkuIntact() {
        return this.skuPeding.isIntact()
    }

    getCurrentValues() {
        return this.skuPeding.getCurrentSpecValues()
    }

    getMissingKeys() {
        const missingKeysIndex = this.skuPeding.getMissingSpecKeyIndex()
        return missingKeysIndex.map(i => {
            return this.fenceGroup.fences[i].title
        })
    }

    /**
     * 初始化当前sku已存在的组合 放入字典中
     * sku 已存在规格
     */
    initPathDict() {
        this.fenceGroup.spu.sku_list.forEach(s => {
            const skuCode = new SkuCode(s.code)
            // 是否包含
            this.pathDict = this.pathDict.concat(skuCode.totalSegments)
        })
        console.log(this.pathDict)
    }

    /**
     * 计算sku规格
     * @param cell
     * @param x
     * @param y
     * @param isInit 是否是初始化默认不是
     */
    judge(cell, x, y, isInit = false) {
        if (!isInit) {
            this._changeCurrentCellStatus(cell, x, y)
        }
        this.fenceGroup.eachCell((cell, x, y) => {
            const path = this._findPotentialPath(cell, x, y)
            console.log(path)
            if (!path) {
                return
            }
            // 查看字典中是否存在
            const insIn = this._isInDict(path)
            if (insIn) {
                // 存在则选中
                this.fenceGroup.fences[x].cells[y].status = CellStatus.WAITING
            } else {
                // 不存在不能选择
                this.fenceGroup.fences[x].cells[y].status = CellStatus.FORBIDDEN
            }
        })
    }

    /**
     * 是否存在字典中
     * @param path 字典参数
     * @returns {boolean}
     * @private
     */
    _isInDict(path) {
        // 是否包含
        return this.pathDict.includes(path)
    }

    _changeOtherCellStatus(cell, x, y) {
        const path = this._findPotentialPath(cell, x, y)
        console.log(path)
    }

    getDeterminateSku() {
        const code = this.skuPeding.getSkuCode()
        console.log(code)
        const sku = this.fenceGroup.getSku(code)
        return sku
    }

    /**
     * 查询潜在路径
     * @param cell
     * @param x
     * @param y
     * @returns {string}
     * @private
     */
    _findPotentialPath(cell, x, y) {
        const joiner = new Joiner("#")
        // 每一个cell都有一个待确认路径 不存在禁用 存在可选
        for (let i = 0; i < this.fenceGroup.fences.length; i++) {
            const selected = this.skuPeding.findSelectedCellByX(i)
            if (x === i) {
                // 当前行
                // 存在就return
                if (this.skuPeding.isSelected(cell, x)) {
                    return
                }
                // cell id 1-42
                const cellCode = this._getCellCode(cell.spec)
                joiner.join(cellCode)
            } else {
                // 其他行
                if (selected) {
                    const selectedCellCode = this._getCellCode(selected.spec)
                    joiner.join(selectedCellCode)
                }
            }
        }
        return joiner.getStr();
    }

    _getCellCode(spec) {
        return spec.key_id + '-' + spec.value_id
    }

    /**
     * 改变当前cell状态
     * @param cell
     * @param x row
     * @param y col
     * @private
     */
    _changeCurrentCellStatus(cell, x, y) {
        if (cell.status === CellStatus.WAITING) {
            // this.fenceGroup.fences[x].cells[y].status = CellStatus.SELECTED
            this.fenceGroup.setCellStatusByXY(x, y, CellStatus.SELECTED)
            this.skuPeding.insertCell(cell, x)
        }
        if (cell.status === CellStatus.SELECTED) {
            // 封装成方法 可读性更加好
            // this.fenceGroup.fences[x].cells[y].status = CellStatus.WAITING
            this.fenceGroup.setCellStatusByXY(x, y, CellStatus.WAITING)
            this.skuPeding.removeCell(x)
        }
    }
}

export {
    Judger
}